<template>
  <div class="login-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <h1>学生管理系统</h1>
        </div>
      </template>
      <el-form @submit.prevent="login" :model="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="form.username" required></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="form.password" required></el-input>
        </el-form-item>
        <el-form-item label="账号类型">
          <el-select v-model="form.accountType" placeholder="请选择账号类型" required style="width: 100%;">
            <el-option label="学生" value="student"></el-option>
            <el-option label="教师" value="teacher"></el-option>
            <el-option label="管理员" value="admin"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit" @click="login">登录</el-button>
          <el-button type="text" @click="register">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';


export default {
  data() {
    return {
      form: {
        username: '',
        password: '',
        accountType: ''
      }
    };
  },
  methods: {
    login() {
      // 这里可以添加登录逻辑
      //console.log('登录:', this.form.username, this.form.password, this.form.accountType);
      
      axios.post("/api/user/login",{
        username: this.form.username,
        password: this.form.password,
       
      }).then(response => {
      
        //本地存储userId进行在组件中共享
        localStorage.setItem("userId",response.data.pkUid)
       // console.log("userId="+response.data.pkUid)

      if (this.form.accountType === 'student' && response.data.roleId == 3) {
        this.$router.push({ name: 'StudentHome'});
      } else if (this.form.accountType === 'teacher' && response.data.roleId == 2) {
        this.$router.push({ name: 'TeacherHome' });
      } else if (this.form.accountType === 'admin' && response.data.roleId ==1 ) {
        this.$router.push({ name: 'AdminHome' });
      }
      }).catch(err => {
          if(err.response && err.response.data){//表示有err.response,并且有err.response.data
              this.$message.error(err.response.data)
          }
      })

      
    },
    register() {
      if (this.form.accountType === 'student') {
        this.$router.push('/student-register');
      } else if (this.form.accountType === 'teacher') {
        this.$router.push('/teacher-register');
      } else if (this.form.accountType === 'admin') {
        this.$message.error('管理员无法注册');
      } else {
        this.$message.error('请选择账号类型');
      }
    }
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: #2d3a4b;
}

.box-card {
  width: 400px;
}

.card-header {
  text-align: center;
}
</style>
